package com.hejia.alauda.service;

import com.hejia.alauda.entity.StatutoryDays;
import com.hejia.alauda.entity.example.StatutoryDaysExample;
import com.hejia.alauda.persistence.mybatis.StatutoryDaysMapper;
import com.hejia.alauda.utils.Pager;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * service - com.hejia.alauda.service Created by clt on 15/3/23.下午5:01
 */
@Service
public class CalendarServiceImpl implements CalendarService {

	@Resource
	private StatutoryDaysMapper statutoryDaysMapper;

	@Override
	public boolean isStatutoryHolidays(String date) {
		StatutoryDaysExample example = new StatutoryDaysExample();
		example.appendCriterion("str_to_date(statutoryDays,'%Y-%m-%d')=", date);
		example.appendCriterion("type =", "STATUTORY_HOLIDAYS");
		StatutoryDays statutoryDays = statutoryDaysMapper
				.selectOneByExample(example);

		return statutoryDays != null;
	}

	@Override
	public boolean isStatutoryWorkOnHolidays(String date) {
		StatutoryDaysExample example = new StatutoryDaysExample();
		example.appendCriterion("str_to_date(statutoryDays,'%Y-%m-%d')=", date);
		example.appendCriterion("type =", "STATUTORY_HOLIDAYS_WORK");
		StatutoryDays statutoryDays = statutoryDaysMapper
				.selectOneByExample(example);

		return statutoryDays != null;
	}

	@Override
	public boolean addDays(StatutoryDays statutoryDays) {
		return statutoryDaysMapper.insert(statutoryDays) > 0;
	}

	@Override
	public List<StatutoryDays> listDaysByMonth(String date) {
		StatutoryDaysExample example = new StatutoryDaysExample();
		example.appendCriterion("str_to_date(statutoryDays,'%Y-%m')=", date
				+ "-00");
		Pager<StatutoryDays> pager = new Pager<>();
		pager.setPageSize(40);
		return statutoryDaysMapper.selectByExample(example, pager);
	}

	@Override
	public boolean removeDays(String date) {
		statutoryDaysMapper.removeDay(date);
		return true;
	}

	@Override
	public boolean isHoliday(String sDate, int term) {
		if (sDate == null || term <= 0) {
			return false;
		}

		try {
			Date date = DateFormatUtils.ISO_DATE_FORMAT.parse(sDate.substring(
					0, 10));
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.DATE, term);
			int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
			if (dayOfWeek == Calendar.SUNDAY || dayOfWeek == Calendar.SATURDAY) {
				return true;
			}

			Date date1 = calendar.getTime();
			return isStatutoryHolidays(DateFormatUtils.ISO_DATE_FORMAT
					.format(date1));

		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean isExist(String date) {
		StatutoryDaysExample example = new StatutoryDaysExample();
		example.appendCriterion("str_to_date(statutoryDays,'%Y-%m-%d')=", date);
		StatutoryDays statutoryDays = statutoryDaysMapper
				.selectOneByExample(example);

		return statutoryDays != null;
	}

    @Override
    public boolean isHoliday(String dateStr) {
        if (isStatutoryHolidays(dateStr)){
            return true;
        }
        if (isStatutoryWorkOnHolidays(dateStr)){
            return false;
        }
        try {
            Date date = DateFormatUtils.ISO_DATE_FORMAT.parse(dateStr);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
            if (dayOfWeek == Calendar.SUNDAY || dayOfWeek == Calendar.SATURDAY) {
                return true;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }

	@Override
	public boolean isHoliday(Date date) {
		return this.isHoliday(DateFormatUtils.format(date, "yyyy-MM-dd"));
	}
}
